require('web-streams-polyfill')
require('dotenv').config({ path: `./.env.${process.env.NODE_ENV.trim()}` }) // 环境变量注入
const { addAliases } = require('module-alias')
// 配置路径别名
addAliases({
    '@': `${__dirname}/src`,
})
const Koa = require('koa')
const fs = require('fs')
const bodyparser = require('koa-bodyparser')
const static = require('koa-static')
const path = require('path')
const cors = require('koa-cors')
const jwt = require('@/utils/jwt')
const router = require('@/utils/router')
const views = require('koa-views')
const schedule = require('node-schedule')
const client = require('@/db/redis')
const logger = require('@/utils/logger')
// require('@/script/ws')
const { SERVICE_PORT } = process.env

const app = new Koa()

// 处理跨域
app.use(cors())

// 自定义日志中间件
app.use(async (ctx,next) => {
    ctx.logger = logger
    await next()
})

// ssr服务端模板渲染
app.use(views(__dirname + '/src/views/ejs', { extension: 'ejs' }))
// 处理post请求
app.use(bodyparser())

// 静态资源
app.use(static(path.resolve(__dirname, './src/views/pages')))

// 权限校验
// app.use(jwt.verify())

app.use(router.routes()).listen(SERVICE_PORT, async () => {
    console.log(`koa-cli服务启动 localhost:${SERVICE_PORT}`)
})
